package com.jbit.hxy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jbit.hxy.common.Result;
import com.jbit.hxy.mapper.WxUserMapper;
import com.jbit.hxy.model.dto.RegisterUserDTO;
import com.jbit.hxy.model.po.WxUser;
import com.jbit.hxy.service.user.WxUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;


@Service
@RequiredArgsConstructor
public class WxUserServiceImpl
        extends ServiceImpl<WxUserMapper, WxUser>
        implements WxUserService {

    private final WxUserMapper wxUserMapper;


    /**
     * 根据openId获取用户信息
     *
     * @param openId
     * @return
     */
    @Transactional(rollbackFor = {Exception.class},readOnly = true,propagation = Propagation.SUPPORTS)
    @Override
    public WxUser getUserByOpenId(String openId) {
        LambdaQueryWrapper<WxUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WxUser::getOpenid,openId);
        return wxUserMapper.selectOne(queryWrapper);
    }

    /**
     * 根据手机号获取用户信息
     *
     * @param phoneNumber
     * @return
     */
    @Transactional(rollbackFor = {Exception.class},readOnly = true,propagation = Propagation.NOT_SUPPORTED)
    @Override
    public WxUser getUserByPhoneNumber(String phoneNumber) {
        LambdaQueryWrapper<WxUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WxUser::getPhoneNumber,phoneNumber);
        return wxUserMapper.selectOne(queryWrapper);
    }

    /**
     * 创建新用户
     * @param user
     * @return
     */
    @Transactional(rollbackFor = {Exception.class}, propagation = Propagation.REQUIRED)
    @Override
    public int createUser(WxUser user) {
        return wxUserMapper.insert(user);
    }



}
